package com.fone.player.online.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.client.TopspotRst.Cnt;
import com.fone.player.client.TopspotRst.Cnts;
import com.fone.player.client.TopspotRst.Module;
import com.fone.player.entity.RedirectInfo;
import com.fone.player.online.activity.LeaderboardActivity;
import com.fone.player.util.Activity2poster;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class LeaderboardColumnAdapter extends BaseAdapter {
	
	/** 日志标签 */
	private static final String TAG = "LeaderboardColumnAdapter";
	private String shost;
	private String host;
	private LayoutInflater layoutInflater;
	private Activity mActivity;
	
	DisplayImageOptions options1;
	DisplayImageOptions options2;
	
	private List<Module> modules;

	public LeaderboardColumnAdapter(Activity activity, String shost, String host) {
		super();
		mActivity = activity;
		
		options1 = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.default_720_340)
		.showImageForEmptyUri(R.drawable.default_720_340)
		.showImageOnFail(R.drawable.default_720_340)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.build();
		
		options2 = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.default_340_192)
		.showImageForEmptyUri(R.drawable.default_340_192)
		.showImageOnFail(R.drawable.default_340_192)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.build();
		
		layoutInflater = LayoutInflater.from(activity);
	}

	/**
	 * 接受数据
	 */
	public void initData(List<Module> module, String shost, String host) {
		this.shost = shost;
		this.host = host;
		this.modules = module;
	}

	@Override
	public int getCount() {
		if(modules==null){
			return 0;
		}
		return modules.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder vHolder = null;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.item_leaderboard_column_list, null, false);
			//创建 ViewHodler 对象
			vHolder = new ViewHolder();
			//初始化条目视图
			iniItemView(convertView, vHolder);
			// 设置 Tag
			convertView.setTag(vHolder);
		} else {
			vHolder = (ViewHolder) convertView.getTag();
		}
		
		//TODO 设置数据
		iniItemData(position,vHolder);
		
		//设置条目的边距
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) vHolder.fourthItem.getLayoutParams();
		if(position==getCount()-1){
			lp.setMargins(0, lp.topMargin, 0, lp.topMargin);
		}else{
			lp.setMargins(0, lp.topMargin, 0, 0);
		}
		vHolder.fourthItem.setLayoutParams(lp);
		
		return convertView;
	}

	
	/**
	 * 初始化条目视图
	 * @param holder
	 */
	private void iniItemView(View convertView,ViewHolder holder){
		
		holder.title = (TextView) convertView.findViewById(R.leaderboard.column_item_title_name);
		holder.more = (TextView) convertView.findViewById(R.leaderboard.column_item_title_more);
		
		holder.firstItem = convertView.findViewById(R.leaderboard.column_item_first);
		holder.firstPic = (ImageView) convertView.findViewById(R.leaderboard.column_item_first_pic);
		holder.firstSignPic = (ImageView) convertView.findViewById(R.leaderboard.first_item_sign);
		holder.firstVidioTitle = (TextView) convertView.findViewById(R.leaderboard.column_item_first_name);
		holder.firstPlayTimes = (TextView) convertView.findViewById(R.leaderboard.column_item_first_play_times);
		
		holder.secendItem = convertView.findViewById(R.leaderboard.column_item_secend);
		holder.secendPic = (ImageView) holder.secendItem.findViewById(R.leaderboard.pic);
		holder.secendSignPic = (ImageView) holder.secendItem.findViewById(R.leaderboard.item_sign);
		holder.secendPlayTimes = (TextView) holder.secendItem.findViewById(R.leaderboard.item_play_times);
		holder.secendVidioTitle = (TextView) holder.secendItem.findViewById(R.leaderboard.name);
		holder.secendContent = (TextView) holder.secendItem.findViewById(R.leaderboard.content);
		
		holder.thirdItem = convertView.findViewById(R.leaderboard.column_item_third);
		holder.thirdPic = (ImageView) holder.thirdItem.findViewById(R.leaderboard.pic);
		holder.thirdSignPic = (ImageView) holder.thirdItem.findViewById(R.leaderboard.item_sign);
		holder.thirdPlayTimes = (TextView) holder.thirdItem.findViewById(R.leaderboard.item_play_times);
		holder.thirdVidioTitle = (TextView) holder.thirdItem.findViewById(R.leaderboard.name);
		holder.thirdContent = (TextView) holder.thirdItem.findViewById(R.leaderboard.content);
		
		holder.fourthItem = convertView.findViewById(R.leaderboard.column_item_fourth);
		holder.fourthPic = (ImageView) holder.fourthItem.findViewById(R.leaderboard.pic);
		holder.fourthSignPic = (ImageView) holder.fourthItem.findViewById(R.leaderboard.item_sign);
		holder.fourthPlayTimes = (TextView) holder.fourthItem.findViewById(R.leaderboard.item_play_times);
		holder.fourthVidioTitle = (TextView) holder.fourthItem.findViewById(R.leaderboard.name);
		holder.fourthContent = (TextView) holder.fourthItem.findViewById(R.leaderboard.content);
		
		holder.itemClickListener = new MyItemClickListener();
		
		holder.more.setOnClickListener(holder.itemClickListener);
		holder.firstItem.setOnClickListener(holder.itemClickListener);
		holder.secendItem.setOnClickListener(holder.itemClickListener);
		holder.thirdItem.setOnClickListener(holder.itemClickListener);
		holder.fourthItem.setOnClickListener(holder.itemClickListener);
		
	}
	
	/**
	 * 填充条目数据
	 * @param holder
	 */
	private void iniItemData(int position, ViewHolder holder){
		holder.secendSignPic.setBackgroundResource(R.drawable.sign_top2);
		holder.thirdSignPic.setBackgroundResource(R.drawable.sign_top3);
		holder.fourthSignPic.setBackgroundResource(R.drawable.sign_top4);
		
		holder.title.setText(modules.get(position).name);
		
		Cnts cnts = modules.get(position).cnts;
		if(cnts!=null){
			List<Cnt> cntList = cnts.cntList;
			holder.itemClickListener.setData(modules.get(position).url,modules.get(position).name,cnts.cntList);
			
			if(cntList.size()>=1){
				// 设置图片
				ImageLoader.getInstance().displayImage(FoneUtil.getAbsoluteUrl(host, shost, cntList.get(0).pic1), holder.firstPic, options1);
				holder.firstVidioTitle.setText(cntList.get(0).name);
				setPlayTimes(holder.firstPlayTimes, cntList.get(0).showcount);
			}
			
			if(cntList.size()>=2){
				// 设置图片
				ImageLoader.getInstance().displayImage(FoneUtil.getAbsoluteUrl(host, shost, cntList.get(1).pic2), holder.secendPic, options2);
				setPlayTimes(holder.secendPlayTimes, cntList.get(1).showcount);
				holder.secendVidioTitle.setText(cntList.get(1).name);
				holder.secendContent.setText(cntList.get(1).desc);
			}
			
			if(cntList.size()>=3){
				// 设置图片
				ImageLoader.getInstance().displayImage(FoneUtil.getAbsoluteUrl(host, shost, cntList.get(2).pic2), holder.thirdPic, options2);
				setPlayTimes(holder.thirdPlayTimes, cntList.get(2).showcount);
				holder.thirdVidioTitle.setText(cntList.get(2).name);
				holder.thirdContent.setText(cntList.get(2).desc);
			}
			
			if(cntList.size()>=4){
				// 设置图片
				ImageLoader.getInstance().displayImage(FoneUtil.getAbsoluteUrl(host, shost, cntList.get(3).pic2), holder.fourthPic, options2);
				setPlayTimes(holder.fourthPlayTimes, cntList.get(3).showcount);
				holder.fourthVidioTitle.setText(cntList.get(3).name);
				holder.fourthContent.setText(cntList.get(3).desc);
			}
			
		}else{
			holder.itemClickListener.setData(null,null,null);
		}
		

		
	}
	
	private void setPlayTimes(TextView view, String palyTimes){
		
		int showCount = FoneUtil.String2Integer(palyTimes);
		if(showCount>10000){
			view.setText(showCount/10000+"."+showCount/1000%10+"万次");
		}else{
			view.setText(showCount+"次");
		}
	}
	
	
	static class ViewHolder {
		TextView title;//标题
		TextView more;//更多
		
		View firstItem;
		ImageView firstPic;//第一个视频海报图片
		ImageView firstSignPic;//第一个排名图片
		TextView firstVidioTitle;//第一个视频名称
		TextView firstPlayTimes;//第一个播放次数
		
		View secendItem;
		ImageView secendPic;//视频海报图片
		ImageView secendSignPic;//排名图片
		TextView secendPlayTimes;//播放次数
		TextView secendVidioTitle;//视频名称
		TextView secendContent;//视频描述
		
		View thirdItem;
		ImageView thirdPic;//视频海报图片
		ImageView thirdSignPic;//排名图片
		TextView thirdPlayTimes;//播放次数
		TextView thirdVidioTitle;//视频名称
		TextView thirdContent;//视频描述
		
		View fourthItem;
		ImageView fourthPic;//视频海报图片
		ImageView fourthSignPic;//排名图片
		TextView fourthPlayTimes;//播放次数
		TextView fourthVidioTitle;//视频名称
		TextView fourthContent;//视频描述
		
		MyItemClickListener itemClickListener;
	}

	/**
	 * 封装播放bean
	 * @param info
	 * @return
	 */
	private RedirectInfo getRedirectInfo(Cnt cnt){
		RedirectInfo redirectInfo = new RedirectInfo();
		redirectInfo.host = host;
		redirectInfo.utp = cnt.utp;
//		redirectInfo.weibourl = cnt.weibourl;//分享的url
		redirectInfo.url = cnt.url;
		redirectInfo.vgurl = cnt.url;
		redirectInfo.xyzplayurl = cnt.url;
//		redirectInfo.dnfs = cnt.dfnt;//清晰度
		redirectInfo.name = cnt.name;
		redirectInfo.ourl = cnt.ourl;//原网页url
		redirectInfo.btnply = cnt.btnply;//原网页是否有按钮
		redirectInfo.pic = cnt.pic2;
		return redirectInfo;
	}
	
	
	private class MyItemClickListener implements OnClickListener{

		private List<Cnt> cnts;
		private String moreUrl;
		private String name;
		
		public void setData(String moreUrl,String name,List<Cnt> cnts){
			this.moreUrl = moreUrl;
			this.name = name;
			this.cnts = cnts;
		}
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.leaderboard.column_item_title_more:
				L.v(TAG, "onClick", "moreurl : " + moreUrl);
				if(!TextUtils.isEmpty(moreUrl)){
					//TODO 跳转到二级页面
					gotoLeaderboard(moreUrl, name);
				}
				break;
			case R.leaderboard.column_item_first:
				L.v(TAG, "onClick", "first item onclick !!!");
				if(cnts!=null&&cnts.size()>=1){
					//TODO 跳转到播放页
					Activity2poster.startActivity(mActivity, getRedirectInfo(cnts.get(0)));
				}
				
				break;
			case R.leaderboard.column_item_secend:
				L.v(TAG, "onClick", "second item onclick !!!");
				if(cnts!=null&&cnts.size()>=2){
					//TODO 跳转到播放页
					Activity2poster.startActivity(mActivity, getRedirectInfo(cnts.get(1)));
				}
				
				break;
			case R.leaderboard.column_item_third:
				L.v(TAG, "onClick", "third item onclick !!!");
				if(cnts!=null&&cnts.size()>=3){
					//TODO 跳转到播放页
					Activity2poster.startActivity(mActivity, getRedirectInfo(cnts.get(2)));
				}
				
				break;
			case R.leaderboard.column_item_fourth:
				L.v(TAG, "onClick", "fourth item onclick !!!");
				if(cnts!=null&&cnts.size()>=4){
					//TODO 跳转到播放页
					Activity2poster.startActivity(mActivity, getRedirectInfo(cnts.get(3)));
				}
				
				break;

			default:
				break;
			}
		}
	}
	
	/**
	 * 跳转至二级栏目页
	 * @param url
	 * @param name
	 */
	private void gotoLeaderboard(String url,String name){
		Intent intent = new Intent(mActivity, LeaderboardActivity.class);
		intent.putExtra("url", url);
		intent.putExtra("name", name);
		mActivity.startActivity(intent);
	}
}
